home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Bug Proofing Visual Basic: A Guide to Error Handling and Prevention
(Publisher: John Wiley & Sons, Inc.)
Author(s): Rod Stephens
ISBN: 0471323519
Publication Date: 11/01/98

Search this book:
 
Previous Table of Contents Next


The following code shows how program Bad5 works. Appendix A, “Self-Test Solutions,” contains one possible improved version of this code. Remember that this is not the only possible solution. You can download updated bad and good versions of this program from the Web at
www.vb-helper.com/err.htm.

Option Explicit

‘ Declare the list of values.
Const NUM_VALUES = 50
Private Values(1 To NUM_VALUES) As Integer

‘ Randomly initialize some values.
Private Sub Form_Load()
    InitValues
End Sub

‘ Randomly initialize some values.
Private Sub InitValues()
Dim i As Integer
Dim txt As String

    ‘ Pick the random values.
    Randomize
    Values(1) = Int(10 * Rnd + 1)
    For i = 2 To NUM_VALUES
        Values(i) = Values(i - 1) + Int(10 * Rnd + 1)
    Next i

    ‘ Display the values.
    For i = 1 To NUM_VALUES
        txt = txt & Format$(i, “@@”) & _
            Format$(Values(i), “@@@@”) & vbCrLf
    Next i
    txtValues.Text = txt
End Sub

‘ Search using the selected method.
Private Sub CmdSearch_Click()
Dim position As Integer
Dim num_searches As Integer

    If optSearchMethod(0).Value Then
    ‘ Linear search.
         position = LinearSearch(Values, _
            CInt(txtTarget.Text), num_searches)
    ElseIf optSearchMethod(1).Value Then
   ‘ Binary search.
        position = BinarySearch(Values, _
            CInt(txtTarget.Text), num_searches)
    End If
    ‘ Display the results.
    If position = 0 Then
        lblPosition.Caption = “Not found”
    Else
        lblPosition.Caption = Format$(position)
    End If
    lblSearches.Caption = Format$(num_searches)
End Sub

‘ Use linear binary search to find the target.
Public Function LinearSearch(list() As Integer, _
    target As Integer, num_searches As Integer) As Integer
Dim i As Integer

    For i = 1 To NUM_VALUES
        num_searches = num_searches + 1
        If Values(i) = target Then Exit For
    Next i

    If i <= NUM_VALUES Then
        LinearSearch = i
    Else
        LinearSearch = 0
    End If
End Function

‘ Use binary search to find the target. For details
‘ of the algorithm, see Ready-to-Run Visual Basic
‘ Algorithms, ISBN 0-471-24268-3.
Public Function BinarySearch(list() As Integer, _
    target As Integer, num_searches As Integer) As Integer
Dim min_index As Integer
Dim max_index As Integer
Dim mid_index As Integer

    min_index = 1
    max_index = NUM_VALUES
    Do While min_index <= max_index
        num_searches = num_searches + 1
        mid_index = (min_index + max_index) / 2
        If target = list(mid_index) Then
            BinarySearch = mid_index
            Exit Function
        ElseIf target < list(mid_index) Then
            max_index = mid_index - 1
        Else
            min_index = mid_index + 1
        End If
    Loop
    BinarySearch = 0
End Function

Summary

Finding bugs can be easy or hard—the choice is yours. If you write code that quietly ignores data that makes no sense, it will hide bugs so finding them will be difficult. If you write code that jumps up and shouts, “There’s a bug here,” whenever it sees a suspicious value, the bugs will come to you. This does not excuse you from testing the code, but it can reduce the number of bugs you need to find during testing.

The following Bug Stoppers list the key bug-exposing concepts described in this chapter.

BUG STOPPERS: Exposing Bugs
Verify arguments using Debug.Assert and Stop.
Validate optional parameters.
Use default values for optional parameters.
Use optional parameters only when necessary.
Use named parameters.
Validate ParamArray parameters.
Stop if a condition should never occur. Raise an error for a bad but possible condition.
Use tight restrictions and loosen them later if necessary.
Use LBound and UBound.
Validate data structures at startup, shutdown, and while the program is running.
Validate data structures in forms and classes.
Clear unused variables or set them to strange values that are easy to recognize.
Use Else statements to watch for unexpected conditions.
Verify results.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.